<html><head><title>ognl.ObjectPropertyAccessor</title></head><body><div id='tit'>ognl.ObjectPropertyAccessor</div><div id='cate'>源码品读</div><div id='date'>2009年12月19日 星期六 10:23 A.M.</div><div id='page'>22</div><a id='url' href='http://hi.baidu.com/hxzon/blog/item/8051fdedb675134478f055a2.html'>http://hi.baidu.com/hxzon/blog/item/8051fdedb675134478f055a2.html</a><div id='cnt'><div> 
 <p> </p> 
 <p><strong>ognl.ObjectPropertyAccessor</strong></p> 
 <p> </p> 
 <h2><font size="-1">ognl</font> <br /> Class ObjectPropertyAccessor</h2> 
 <pre>java.lang.Object
  <img alt="extended by" src="image/ognl.ObjectPropertyA.inherit.gif" /><p class="origImg">http://www.opensymphony.com/ognl/api/resources/inherit.gif</p><strong>ognl.ObjectPropertyAccessor</strong></pre> 
 <dl>
  <dt>
   <strong>All Implemented Interfaces:</strong> 
  </dt>
  <dd>
   PropertyAccessor
  </dd>
 </dl>
 <dl>
  <dt>
   <strong>Direct Known Subclasses:</strong> 
  </dt>
  <dd>
   ArrayPropertyAccessor, EnumerationPropertyAccessor, IteratorPropertyAccessor, ListPropertyAccessor, SetPropertyAccessor
  </dd>
 </dl>
 <dl></dl>
 <dl>
  <dt>
   public class 
   <strong>ObjectPropertyAccessor</strong>
  </dt>
  <dt>
   extends Object
  </dt>
  <dt>
   implements PropertyAccessor
  </dt>
 </dl> 
 <p>Implementation of PropertyAccessor that uses reflection on the target object's class to find a field or a pair of set/get methods with the given property name.</p> 
 <p> </p> 
 <p> </p> 
 <dl>
  <dt>
   <strong>Author:</strong>
  </dt>
  <dd>
   Luke Blanshard (blanshlu@netscape.net), Drew Davidson (drew@ognl.org)
  </dd>
 </dl>
 <dl>
  ----------------------------------------------- 
 </dl> 
 <p><strong><font face="新宋体">getPossibleProperty</font></strong><font face="新宋体">(</font><font face="新宋体">Map</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">String</font><font face="新宋体"> name)</font> <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Returns OgnlRuntime.NotFound if the property does not exist.</p> 
 <p><strong><font face="新宋体">getProperty</font></strong><font face="新宋体">(</font><font face="新宋体">Map</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">Object</font><font face="新宋体"> oname)</font> <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Extracts and returns the property of the given name from the given target object.</p> 
 <p><strong><font face="新宋体">setPossibleProperty</font></strong><font face="新宋体">(</font><font face="新宋体">Map</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">String</font><font face="新宋体"> name, </font><font face="新宋体">Object</font><font face="新宋体"> value)</font> <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Returns OgnlRuntime.NotFound if the property does not exist.</p> 
 <p><strong><font face="新宋体">setProperty</font></strong><font face="新宋体">(</font><font face="新宋体">Map</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">Object</font><font face="新宋体"> oname, </font><font face="新宋体">Object</font><font face="新宋体"> value)</font> <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sets the value of the property of the given name in the given target object.</p> 
 <p>---------------------------------------</p> 
 <h2><font size="-1">ognl</font> <br /> Class OgnlRuntime</h2> 
 <pre>java.lang.Object
  <img alt="extended by" src="image/ognl.ObjectPropertyA.inherit.gif" /><p class="origImg">http://www.opensymphony.com/ognl/api/resources/inherit.gif</p><strong>ognl.OgnlRuntime</strong></pre> 
 <p>------------------</p> 
 <p><strong><font face="新宋体">getMethodValue</font></strong><font face="新宋体">(</font><font face="新宋体">OgnlContext</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">String</font><font face="新宋体"> propertyName)</font></p> 
 <p><strong><font face="新宋体">getMethodValue</font></strong><font face="新宋体">(</font><font face="新宋体">OgnlContext</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">String</font><font face="新宋体"> propertyName, boolean checkAccessAndExistence)</font> <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If the checkAccessAndExistence flag is true this method will check to see if the method exists and if it is accessible according to the context's MemberAccess.</p> 
 <p><strong><font face="新宋体">getGetMethod</font></strong><font face="新宋体">(</font><font face="新宋体">OgnlContext</font><font face="新宋体"> context, </font><font face="新宋体">Class</font><font face="新宋体"> targetClass, </font><font face="新宋体">String</font><font face="新宋体"> propertyName)</font></p> 
 <p><strong><font face="新宋体">getFieldValue</font></strong><font face="新宋体">(</font><font face="新宋体">OgnlContext</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">String</font><font face="新宋体"> propertyName)</font></p> 
 <p><strong><font face="新宋体">getFieldValue</font></strong><font face="新宋体">(</font><font face="新宋体">OgnlContext</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">String</font><font face="新宋体"> propertyName, boolean checkAccessAndExistence)</font></p> 
 <p><strong><font face="新宋体">setMethodValue</font></strong><font face="新宋体">(</font><font face="新宋体">OgnlContext</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">String</font><font face="新宋体"> propertyName, </font><font face="新宋体">Object</font><font face="新宋体"> value)</font></p> 
 <p><strong><font face="新宋体">setMethodValue</font></strong><font face="新宋体">(</font><font face="新宋体">OgnlContext</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">String</font><font face="新宋体"> propertyName, </font><font face="新宋体">Object</font><font face="新宋体"> value, boolean checkAccessAndExistence)</font></p> 
 <p><strong><font face="新宋体">getSetMethod</font></strong><font face="新宋体">(</font><font face="新宋体">OgnlContext</font><font face="新宋体"> context, </font><font face="新宋体">Class</font><font face="新宋体"> targetClass, </font><font face="新宋体">String</font><font face="新宋体"> propertyName)</font></p> 
 <p><strong><font face="新宋体">setFieldValue</font></strong><font face="新宋体">(</font><font face="新宋体">OgnlContext</font><font face="新宋体"> context, </font><font face="新宋体">Object</font><font face="新宋体"> target, </font><font face="新宋体">String</font><font face="新宋体"> propertyName, </font><font face="新宋体">Object</font><font face="新宋体"> value)</font></p> 
 <p>===================</p> 
 <p>//--------------------------------------------------------------------------<br /> // Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard<br /> // All rights reserved.<br /> //<br /> // Redistribution and use in source and binary forms, with or without<br /> // modification, are permitted provided that the following conditions are<br /> // met:<br /> //<br /> // Redistributions of source code must retain the above copyright notice,<br /> // this list of conditions and the following disclaimer.<br /> // Redistributions in binary form must reproduce the above copyright<br /> // notice, this list of conditions and the following disclaimer in the<br /> // documentation and/or other materials provided with the distribution.<br /> // Neither the name of the Drew Davidson nor the names of its contributors<br /> // may be used to endorse or promote products derived from this software<br /> // without specific prior written permission.<br /> //<br /> // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS<br /> // &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT<br /> // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS<br /> // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE<br /> // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,<br /> // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,<br /> // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS<br /> // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED<br /> // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,<br /> // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF<br /> // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH<br /> // DAMAGE.<br /> //--------------------------------------------------------------------------<br /> package ognl;</p> 
 <p>import java.beans.IntrospectionException;<br /> import java.util.Map;</p> 
 <p>/**<br /> * Implementation of PropertyAccessor that uses reflection on the target object's class to<br /> * find a field or a pair of set/get methods with the given property name.<br /> * @author Luke Blanshard (blanshlu@netscape.net)<br /> * @author Drew Davidson (drew@ognl.org)<br /> */<br /> public class ObjectPropertyAccessor implements PropertyAccessor<br /> {<br /> &nbsp;&nbsp;&nbsp; /**<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Returns OgnlRuntime.NotFound if the property does not exist.<br /> &nbsp;&nbsp;&nbsp;&nbsp; */<br /> &nbsp;&nbsp;&nbsp; public Object getPossibleProperty( Map context, Object target, String name) throws OgnlException<br /> &nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OgnlContext&nbsp;&nbsp;&nbsp;&nbsp; ognlContext = (OgnlContext)context;</p> 
 <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((result = OgnlRuntime.getMethodValue(ognlContext, target, name, true)) == OgnlRuntime.NotFound) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = OgnlRuntime.getFieldValue(ognlContext, target, name, true);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IntrospectionException ex) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new OgnlException(name, ex);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (OgnlException ex) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw ex;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception ex) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new OgnlException(name, ex);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result;<br /> &nbsp;&nbsp;&nbsp; }</p> 
 <p>&nbsp;&nbsp;&nbsp; /**<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Returns OgnlRuntime.NotFound if the property does not exist.<br /> &nbsp;&nbsp;&nbsp;&nbsp; */<br /> &nbsp;&nbsp;&nbsp; public Object setPossibleProperty( Map context, Object target, String name, Object value) throws OgnlException<br /> &nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = null;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OgnlContext&nbsp;&nbsp;&nbsp;&nbsp; ognlContext = (OgnlContext)context;</p> 
 <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!OgnlRuntime.setMethodValue(ognlContext, target, name, value, true)) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = OgnlRuntime.setFieldValue(ognlContext, target, name, value) ? null : OgnlRuntime.NotFound;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IntrospectionException ex) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new OgnlException(name, ex);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (OgnlException ex) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw ex;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception ex) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new OgnlException(name, ex);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result;<br /> &nbsp;&nbsp;&nbsp; }</p> 
 <p>&nbsp;&nbsp;&nbsp; public boolean hasGetProperty( OgnlContext context, Object target, Object oname ) throws OgnlException<br /> &nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return OgnlRuntime.hasGetProperty( context, target, oname );<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IntrospectionException ex) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new OgnlException(&quot;checking if &quot; + target + &quot; has gettable property &quot; + oname, ex);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp; }</p> 
 <p>&nbsp;&nbsp;&nbsp; public boolean hasGetProperty( Map context, Object target, Object oname ) throws OgnlException<br /> &nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return hasGetProperty((OgnlContext)context, target, oname);<br /> &nbsp;&nbsp;&nbsp; }</p> 
 <p>&nbsp;&nbsp;&nbsp; public boolean hasSetProperty( OgnlContext context, Object target, Object oname ) throws OgnlException<br /> &nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return OgnlRuntime.hasSetProperty( context, target, oname );<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IntrospectionException ex) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new OgnlException(&quot;checking if &quot; + target + &quot; has settable property &quot; + oname, ex);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp; }</p> 
 <p>&nbsp;&nbsp;&nbsp; public boolean hasSetProperty( Map context, Object target, Object oname ) throws OgnlException<br /> &nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return hasSetProperty((OgnlContext)context, target, oname);<br /> &nbsp;&nbsp;&nbsp; }</p> 
 <p>&nbsp;&nbsp;&nbsp; public Object getProperty( Map context, Object target, Object oname ) throws OgnlException<br /> &nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = null;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = oname.toString();</p> 
 <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((result = getPossibleProperty(context, target, name)) == OgnlRuntime.NotFound) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new NoSuchPropertyException(target, name);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result;<br /> &nbsp;&nbsp;&nbsp; }</p> 
 <p>&nbsp;&nbsp;&nbsp; public void setProperty( Map context, Object target, Object oname, Object value ) throws OgnlException<br /> &nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = oname.toString();</p> 
 <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (setPossibleProperty(context, target, name, value) == OgnlRuntime.NotFound) {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new NoSuchPropertyException(target, name);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp; }<br /> }</p> 
 <p> </p> 
 <p> </p> 
</div></div></body></html>